import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class StockQuantEdit extends StatefulWidget {
  StockQuantEdit({this.stockQuant});

  final StockQuant? stockQuant;

  @override
  _StockQuantEditState createState() => _StockQuantEditState();
}

class _StockQuantEditState extends State<StockQuantEdit> {
  late StockQuant stockQuant;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

   //仓库位置id
  List<SelectOptionVO> locationIdList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.stockQuant == null;
    this.stockQuant = widget.stockQuant ?? StockQuant();
    _loadSelectVO();
  }

   _loadSelectVO() async{
   //仓库位置id
   locationIdList = await DynamicDataApi.getDictList("location_id");

    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '产品id',
            value: stockQuant.productId,
            onSaved: (v) {
              stockQuant.productId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CrySelect(
            label: '仓库位置id',
            dataList: locationIdList,
            value: stockQuant.locationId,
            onSaved: (v) => { stockQuant.locationId = v},
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInputNum(
            label: '库存数量',
            value: stockQuant.quantity,
            onSaved: (v) {
              stockQuant.quantity = v;
            },
          ),

          CryInput(
            label: '车间',
            value: stockQuant.workshopId,
            onSaved: (v) {
              stockQuant.workshopId = v;
            },
          ),

          CryInput(
            label: 'create_uid',
            value: stockQuant.createUid,
            onSaved: (v) {
              stockQuant.createUid = v;
            },
          ),

          CryInput(
            label: 'write_uid',
            value: stockQuant.writeUid,
            onSaved: (v) {
              stockQuant.writeUid = v;
            },
          ),

           CrySelectDate(
            context,
            label: 'write_date',
            value: stockQuant.writeDate,
            onSaved: (v) {
              stockQuant.writeDate = v;
            },
          ),

          CryInput(
            label: '生产单号',
            value: stockQuant.productorderno,
            onSaved: (v) {
              stockQuant.productorderno = v;
            },
          ),

          CryInput(
            label: '批次号',
            value: stockQuant.batchno,
            onSaved: (v) {
              stockQuant.batchno = v;
            },
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            StockQuantOdooApi.saveOrUpdate(stockQuant.toOdooMap()).then((ResponseBodyApi res) {
              if (!res.success!) {
               //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}